home *** CD-ROM | disk | FTP | other *** search
/ Net Connect / NetConnect - Version 1.iso / mui / developer / c / examples / brush2c.c < prev    next >
C/C++ Source or Header  |  1997-02-11  |  4KB  |  152 lines

  1. /*
  2. ** This little program can be used to convert an ILBM brush
  3. ** to something that MUI's Bodychunk class can understand.
  4. **
  5. ** Perfect for about logos and stuff like that...
  6. **
  7. ** Have fun,
  8. ** Stefan.
  9. */
  10.  
  11. #include "exec/types.h"
  12. #include "exec/memory.h"
  13. #include "libraries/dos.h"
  14. #include "libraries/iffparse.h"
  15. #include "datatypes/pictureclass.h"
  16. #include "graphics/gfx.h"
  17. #include "proto/dos.h"
  18. #include "proto/iffparse.h"
  19. #include "stdlib.h"
  20. #include "string.h"
  21. #include "stdio.h"
  22. #include "ctype.h"
  23.  
  24.  
  25. #define to32(c) (((c)<<24)|((c)<<16)|((c)<<8)|(c))
  26.  
  27. #define ID_ANIM MAKE_ID('A','N','I','M')
  28. #define ID_ANHD MAKE_ID('A','N','H','D')
  29. #define ID_DLTA MAKE_ID('D','L','T','A')
  30.  
  31.  
  32. char name[100],uname[100];
  33.  
  34.  
  35. BOOL ParseILBM(struct IFFHandle *iff)
  36. {
  37.     struct StoredProperty *sp;
  38.     struct BitMapHeader *bmhd;
  39.     UBYTE *body;
  40.     int size,i;
  41.     UBYTE *cols;
  42.     BOOL rc = FALSE;
  43.  
  44.     if (!PropChunk(iff,ID_ILBM,ID_BMHD) &&
  45.         !PropChunk(iff,ID_ILBM,ID_CMAP) &&
  46.         !StopChunk(iff,ID_ILBM,ID_BODY) &&
  47.         !StopOnExit(iff,ID_ILBM,ID_FORM) &&
  48.         !ParseIFF(iff,IFFPARSE_SCAN))
  49.     {
  50.         if (sp=FindProp(iff,ID_ILBM,ID_CMAP))
  51.         {
  52.             cols = (UBYTE *)sp->sp_Data;
  53.  
  54.             printf("#ifdef USE_%s_COLORS\n",uname);
  55.             printf("const ULONG %s_colors[%ld] =\n{\n",name,sp->sp_Size);
  56.             for (i=0;i<sp->sp_Size;i+=3)
  57.                 printf("\t0x%08lx,0x%08lx,0x%08lx,\n",to32(cols[i]),to32(cols[i+1]),to32(cols[i+2]));
  58.             printf("};\n");
  59.             printf("#endif\n\n");
  60.         }
  61.  
  62.         if (sp=FindProp(iff,ID_ILBM,ID_BMHD))
  63.         {
  64.             bmhd = (struct BitMapHeader *)sp->sp_Data;
  65.  
  66.             if ((bmhd->bmh_Compression==cmpNone) || (bmhd->bmh_Compression==cmpByteRun1))
  67.             {
  68.                 size = CurrentChunk(iff)->cn_Size;
  69.  
  70.                 if (body=malloc(size))
  71.                 {
  72.                     if (ReadChunkBytes(iff,body,size)==size)
  73.                     {
  74.                         fprintf(stderr,"Width %d Height %d Depth %d - converting...\n",bmhd->bmh_Width,bmhd->bmh_Height,bmhd->bmh_Depth);
  75.  
  76.                         printf("#define %s_WIDTH       %3d\n",uname,bmhd->bmh_Width);
  77.                         printf("#define %s_HEIGHT      %3d\n",uname,bmhd->bmh_Height);
  78.                         printf("#define %s_DEPTH       %3d\n",uname,bmhd->bmh_Depth);
  79.                         printf("#define %s_COMPRESSION %3d\n",uname,bmhd->bmh_Compression);
  80.                         printf("#define %s_MASKING     %3d\n",uname,bmhd->bmh_Masking);
  81.                         printf("\n");
  82.  
  83.                         printf("#ifdef USE_%s_HEADER\n",uname);
  84.                         printf("const struct BitMapHeader %s_header =\n{ %ld,%ld,%ld,%ld,%ld,%ld,%ld,0,%ld,%ld,%ld,%ld,%ld };\n",name,bmhd->bmh_Width,bmhd->bmh_Height,bmhd->bmh_Left,bmhd->bmh_Top,bmhd->bmh_Depth,bmhd->bmh_Masking,bmhd->bmh_Compression,bmhd->bmh_Transparent,bmhd->bmh_XAspect,bmhd->bmh_YAspect,bmhd->bmh_PageWidth,bmhd->bmh_PageHeight);
  85.                         printf("#endif\n\n");
  86.  
  87.                         printf("#ifdef USE_%s_BODY\n",uname);
  88.                         printf("const UBYTE %s_body[%ld] = {\n",name,size);
  89.                         for (i=0;i<size;i++)
  90.                         {
  91.                             printf("0x%02lx,",body[i]);
  92.                             if (!((i+1)%15)) printf("\n");
  93.                         }
  94.                         printf(" };\n");
  95.                         printf("#endif\n");
  96.  
  97.                         rc = TRUE;
  98.                     }
  99.                     free(body);
  100.                 }
  101.             }
  102.         }
  103.     }
  104.     return(rc);
  105. }
  106.  
  107.  
  108. int main(int argc,char **argv)
  109. {
  110.     char *c;
  111.     struct IFFHandle *iff;
  112.     struct ContextNode *cn;
  113.  
  114.     if (argc<2 || argc>3 || argv[1][0]=='?')
  115.     {
  116.         printf("Syntax: %s ilbm-file\n",argv[0]);
  117.         exit(20);
  118.     }
  119.  
  120.     stccpy(name,FilePart(argv[1]),100);
  121.     if (c=strchr(name,'.')) *c=0;
  122.     strcpy(uname,name);
  123.     for (c=uname;*c;c++)
  124.         *c=toupper(*c);
  125.  
  126.     if (iff=AllocIFF())
  127.     {
  128.         if (iff->iff_Stream=Open(argv[1],MODE_OLDFILE))
  129.         {
  130.             InitIFFasDOS(iff);
  131.  
  132.             if (!OpenIFF(iff,IFFF_READ))
  133.             {
  134.                 if (!ParseIFF(iff,IFFPARSE_STEP))
  135.                 {
  136.                     if ((cn=CurrentChunk(iff)) && (cn->cn_ID==ID_FORM))
  137.                     {
  138.                         if (cn->cn_Type==ID_ILBM)
  139.                         {
  140.                             ParseILBM(iff);
  141.                         }
  142.                     }
  143.                 }
  144.                 CloseIFF(iff);
  145.             }
  146.             Close(iff->iff_Stream);
  147.         }
  148.         FreeIFF(iff);
  149.     }
  150.     return(0);
  151. }
  152.